package com.aiyige.page.pay;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.ApiManager;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.Page;
import com.aiyige.page.coupon.model.UserCoupon;
import com.aiyige.page.coupon.util.CouponUtil;
import com.aiyige.page.pay.adapter.ChooseCouponAdapter;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.ChooseCouponPage)
public class ChooseCouponPage extends BaseActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.actionBtn)
    Button nonuseCouponBtn;

    @BindView(R.id.titleTv)
    TextView titleTv;

    @BindView(R.id.cdv)
    CommonDataView cdv;
    ChooseCouponAdapter adapter;
    @BindView(R.id.rl_nonuse_coupon)
    RelativeLayout relativeLayoutNoUser;
    @BindView(R.id.tv_nouse)
    TextView tvNoUse;

    @Autowired
    String momentId;

    @Autowired
    double fullAmount;

    public final static int CONFIRM_ORDER_PAGE_REQUEST_CODE = 10;

    public final static int NO_USE = 11;

    List<UserCoupon> userCouponList;

    @Autowired
    UserCoupon selectedUserCoupon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_choose_coupon);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.choose_coupon);
        adapter = new ChooseCouponAdapter();
        cdv.config(adapter, this);
        cdv.addItemDecoration(15, R.color.zwcolor_gray_f5f7f9);
        cdv.doRefreshRequest();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UserCoupon userCoupon = ChooseCouponPage.this.adapter.getData().get(position);
                if (!TextUtils.isEmpty(userCoupon.getCurrentMomentAvailable()) && userCoupon.getCurrentMomentAvailable().equals(CouponUtil.CURRENT_STATUS_AVAILABLE)) {
                    selectedUserCoupon = userCoupon;
                    refreshCouponList(position);
                    Intent intent = new Intent();
                    intent.putExtra("userCoupon", selectedUserCoupon);
                    setResult(CONFIRM_ORDER_PAGE_REQUEST_CODE, intent);
                    finish();
                }
            }
        });
        if (selectedUserCoupon == null) {
            relativeLayoutNoUser.setEnabled(false);
            tvNoUse.setTextColor(ContextCompat.getColor(this,R.color.zwcolor_gray_b1b5c1));
        }
    }


    private void refreshCouponList(int position) {
        for (int i = 0; i < adapter.getData().size(); i++) {
            adapter.getData().get(i).setSelected(i == position);
        }
        adapter.notifyDataSetChanged();
    }


    @OnClick({R.id.titleBackBtn, R.id.rl_nonuse_coupon})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
            case R.id.rl_nonuse_coupon:
                nonuseCouponBtn.setSelected(true);
                refreshCouponList(-1);
                setResult(NO_USE);
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().chooseUserConpons(momentId, fullAmount, 15, currentPage);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            if (!TextUtils.isEmpty(page.getContent())) {
                List<UserCoupon> tempFollowList = JsonUtil.toObjectList(page.getContent(), UserCoupon.class);
                if (selectedUserCoupon != null && !TextUtils.isEmpty(selectedUserCoupon.getId())) {
                    for (UserCoupon userCoupon : tempFollowList) {
                        if (userCoupon.getId().equals(selectedUserCoupon.getId())) {
                            userCoupon.setSelected(true);
                        } else {
                            userCoupon.setSelected(false);
                        }
                    }
                }
                resultList.addAll(tempFollowList);
            }

            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
